/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.domain;

import org.braintrain.domain.types.ResourceType;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @author John Ericksen
 */
@Entity
@Table(name="BRAINTRAIN_RESOURCE_FACADE")
public class ResourceFacade extends BrainTrainBase{

    private static final long serialVersionUID = 8792759514472314631L;

    private BlobFile blobFile;
    private String resourceHandle;
    private Long step;
    private String organization;
    private ResourceType type;
    private Set<ResourceReference> references = new HashSet<ResourceReference>();
    private Set<ResourceReference> resourceReferences = new HashSet<ResourceReference>();

    public ResourceFacade(){
        //empty
    }

    public ResourceFacade(ResourceType type) {
        this.type = type;
    }

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "blob_id")
    public BlobFile getBlobFile() {
        return blobFile;
    }

    public void setBlobFile(BlobFile blobFile) {
        this.blobFile = blobFile;
    }

    public String getResourceHandle() {
        return resourceHandle;
    }

    public void setResourceHandle(String resourceHandle) {
        this.resourceHandle = resourceHandle;
    }

    @Enumerated(value = EnumType.STRING)
    public ResourceType getType() {
        return type;
    }

    public void setType(ResourceType type) {
        this.type = type;
    }

    public Long getStep() {
        return step;
    }

    public void setStep(Long step) {
        this.step = step;
    }

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    @OneToMany(mappedBy = "resource" ,targetEntity=ResourceReference.class, fetch=FetchType.LAZY)
    public Set<ResourceReference> getReferencedResources() {
        return references;
    }

    public void setReferencedResources(Set<ResourceReference> referencedResources) {
        this.references = referencedResources;
    }

    @OneToMany(mappedBy = "resourceReference" ,targetEntity=ResourceReference.class, fetch=FetchType.LAZY)
    public Set<ResourceReference> getResourceReferences() {
        return resourceReferences;
    }

    public void setResourceReferences(Set<ResourceReference> resourceReferences) {
        this.resourceReferences = resourceReferences;
    }

    @Transient
    public String getResourceIdentifier() {
        return type.buildResourceIdentifier(organization, resourceHandle, step);
    }

    @Transient
    public ResourceFacade generateNext(){
        ResourceFacade next = new ResourceFacade();

        next.setOrganization(organization);
        next.setResourceHandle(resourceHandle);
        if(step != null){
            next.setStep(step + 1);
        }
        else{
            next.setStep(1L);
        }
        next.setType(type);

        return next;
    }
}
